package com.rex.saas.controller;

import com.rex.saas.bean.BaseResponse;
import com.rex.saas.bean.ResultCode;
import com.rex.saas.bean.TenantBasicData;
import com.rex.saas.constants.Constants;
import com.rex.saas.service.RexolarApiClient;
import com.rex.saas.utils.JwtUtils;
import lombok.extern.slf4j.Slf4j;
import org.checkerframework.checker.units.qual.A;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @ClassName TenantController
 * @Description:
 * @Author: zhusiyu
 */
@RestController
@Slf4j
@RequestMapping("/tenant")
public class TenantController {
    @Autowired
    private RexolarApiClient rexolarApiClient;
    @GetMapping("/list")
    @ResponseBody
    public BaseResponse list(HttpServletRequest request){
        log.info("TenantController list ======>");
        String token = request.getHeader("token");
        List<TenantBasicData> tenants = new ArrayList<>();
        List<String> roles = JwtUtils.getRole(token);
        if (roles.contains(Constants.TENANT_ADMIN)){
            String tenantId = JwtUtils.getTenantId(token);
            TenantBasicData tenant = rexolarApiClient.getTenantById(tenantId, token);
            tenants.add( tenant);
        } else if (roles.contains(Constants.SYSTEM_ADMIN)){
            tenants = rexolarApiClient.getTenants(token, null);
        }else {
            log.error("list , user has not permission");
            return BaseResponse.error("权限不足", ResultCode.PARAM_ERROR);
        }
        return BaseResponse.ok(tenants);
    }
}
